const express = require('express');

const bodyParser = require('body-parser');
const multer = require('multer');
const fs = require('fs');
const pathLib = require('path');
var objMulter = multer();
var server = express();

//创建multer对象,指定上传目录
var objMulter = multer({dest:'./www/upload/'});
//any 可以接受任何文件
server.use(objMulter.any());

server.use(bodyParser.urlencoded({extended:false}));
server.post('/',function(req,res){
    // console.log(req.files);
    // [ { fieldname: 'username',
    // originalname: '屏幕快照 2017-05-30 上午8.12.11.png',
    // encoding: '7bit',
    // mimetype: 'image/png',
    // buffer: <Buffer 89 50 4e 47 0d 0a 1a 0a 00 00 00 0d 49 48 44 52 00 00 01 10 00 00 01 94 08 06 00 00 00 fd 2b 7d db 00 00 01 5e 69 43 43 50 49 43 43 20 50 72 6f 66 69 ... >,
    // size: 160018 } ]

    //拼接新的文件名
    var pathObj = pathLib.parse(req.files[0].originalname);
    var newName = req.files[0].path + pathObj.ext;
    console.log(newName);
    fs.rename(req.files[0].path,newName,function(err){
        if(err){
            res.send('上传失败');
        }else{
            res.send('成功');
        }
    })

})

server.listen(8080);
